home *** CD-ROM | disk | FTP | other *** search
- /* ARexx script to maintain MakeCat style locale C-code
- *
- * written 20/09/93
- * © 1993 by MGR-Software, Asgard
- * contact: mgr@asgard.bo.open.de
- *
- * Action codes are to be passed as parameters. The following codes*
- * are supported:
- *
- * CUTSTRING - break up a constant string at the cursor position
- * into two lines using the '\' C-directive
- *
- * BREAKSTRING - break up string to a maximum linelength of the
- * current cursor position
- *
- * CATSTRING - place a multiline string into a single line. Note
- * that you may not have trailing "\" in comments!
- * Also not that comments get lost.
- *
- * FIXSTRING - recurse CATSTRING until the string is in a single line
- *
- * LOCALIZE - take a given string and create a LocText structure
- * for it. Count IDs automatically, place the string
- * at the right location or even file, etc.
- * Note that the string must probably FIXSTRINGED first!
- *
- * EDITTEXT - find the LocText structure belonging to the ID under
- * the cursor
- *
- * RENUMBER - renumber all catalog entries
- *
- * Installation:
- *
- * Put the above actions on some keys or menus. Write your locale stuff
- * according to the MakeCat requirements, i.e:
- *
- * struct LocText MSG_TEST={2,"This is a test!"};
- * (*
- * D "Das ist ein Test!"
- * F "Ça c'est un test!"
- * *)
- *
- * where (*, *) denote comments that would confuse ARexx. All those
- * entries must be enclosed in a block started by
- *
- * #define LOCALE_START
- * #define LOCALE_END
- *
- * finishes that block. The file must also contain
- *
- * #define LOCALE_INDEX 0
- *
- * which holds the automatically maintained ID for the next catalog
- * entry. If it does not exist it will be automatically created and
- * set to 0. You may use "RENUMBER" to clean up.
- * You may place
- *
- * $Localefile = myfile
- *
- * e.g. inside a comment, into any file holding locale strings. This
- * script will automatically lookup into the file named there for the
- * catalog entries.
- * Finally you should define a macro called "Loc" anywhere you want.
- * The above example will be accessed by
- *
- * test_string = Loc(MSG_TEST)
- *
- */
-
- arg action
-
- if ~show('P',"WRITE") then
- do
- say "This script is useless without the WRITE editor!"
- exit 0
- end
-
- address 'WRITE'
- options results
-
- 'VERSIONCHECK 31090 "Locale.wrx"'
- IF RC~=0 THEN DO
- exit 10
- END
-
- 'GetVar (_CurrentID)'
- ID = RESULT
- 'LockWindow' ID
-
- select
- when action = "CUTSTRING" then call cutstring
- when action = "CATSTRING" then call catstring ext
- when action = "BREAKSTRING" then call breakstring
- when action = "FIXSTRING" then call fixstring
- when action = "LOCALIZE" then call localize
- when action = "EDITTEXT" then call edittext
- when action = "RENUMBER" then call renumber
- otherwise 'MessageOK (Unbekannte Option:' action ')'
- end
-
- 'LockWindow 0'
- exit 0
-
- renumber: procedure
-
- 'GETVAR (_XPos)' /* get home position */
- a.x = RESULT
- 'GETVAR (_YPos)'
- a.y = RESULT
-
- 'Goto 1 1' /* find locale part of program */
- 'SetVar (_PatCase) (# struct# LocText#?=# {#?)'
- 'SetVar (_FindString) ({)'
- 'GetVar (_WordDef)'
- wdef = RESULT
- 'SetVar (_WordDef) 1' /* as cyphers are not alpha */
- num = 0 /* IDs start with 0 */
- 'FindPattern @CURSOR @EOT {@SILENT}'
- do while RC = 0
- 'Goto @SOL @SOL'
- 'Find @CURSOR @CURSOR @EOT @EOT {@SILENT}'
- do until ((word ~= "") | (ret ~= 0)) /* find id in a word */
- 'CursorRight 0'
- ret = RC
- 'GetVar (_CurrentWord)'
- word = RESULT
- end
- if ret ~= 0 then
- do
- 'MessageOK "Malformed LocText structure. Aborting ..."'
- 'SetVar (_WordDef)' wdef
- return
- end
-
- 'CursorRight 0' /* find beginning of current word */
- 'LastWord'
-
- p=pos('{',word) /* get leading items */
- if p ~= 0 then pre = left(word,p)
- else pre = ""
-
- 'DeleteWord'
- 'WriteText "'||pre num||'"'
- num = num + 1
- 'CursorDown 0'
- if RC = 0 then 'FindPattern @CURSOR @EOT {@SILENT}'
- end
-
- 'Goto 1 1' /* set maximum catalog message number */
- 'SetVar (_FindString) (#define LOCALE_INDEX)'
- 'Find @CURSOR @CURSOR @EOT @EOT {@SILENT}'
- if RC ~= 0 then
- do
- 'Goto 1 1'
- 'WriteText (#define LOCALE_INDEX' num||')'
- 'Return'
- end
- else do
- 'DeleteLine'
- 'WriteText "#define LOCALE_INDEX' num || '"'
- 'Return'
- end
-
- 'SetVar (_WordDef)' wdef
- 'Goto' a.x a.y
-
- return
-
-
- edittext: procedure
-
- 'GETVAR (_XPos)' /* get home position */
- a.x = RESULT
- 'GETVAR (_YPos)'
- a.y = RESULT
-
- 'GetVar (_CurrentWord)' /* get ID */
- topic = RESULT
-
- call gettextfile
- if ret ~= 0 then return
-
- if (OldID ~= 0) then
- do
- 'LockWindow' OldID /* leave the cursor untouched, if the */
- 'Goto' a.x a.y /* locals are in a different file */
- 'LockWindow' ID
- end
-
- 'SetVar (_FindString) (struct LocText ' || topic || '=)'
- 'Find @CURSOR @CURSOR @EOT @EOT {@SILENT}'
- if RC ~= 0 then
- do
- 'MessageOK (Locale string "'|| topic ||'" not found!)'
- if (OldID ~= 0) then 'QuitED'
- end
-
- return
-
- localize: procedure
-
- 'GETVAR (_XPos)' /* get home position */
- a.x = RESULT
- 'GETVAR (_YPos)'
- a.y = RESULT
-
- 'GetVar (_CurrentChar)' /* are we on a string */
- ch = RESULT
- if ch ~= '"' then
- do
- 'MessageOK (Cannot localize constants!)'
- return
- end
- 'UnMark' /* cut string to buffer 1 */
- 'SetMark'
- old = '\'
- do until ((ch = '"') & (old ~= '\'))
- 'CursorRight 1'
- if RC ~= 0 then
- do
- "MessageOK (String isn't terminated)"
- return
- end
- old = ch
- 'GetVar (_CurrentChar)'
- ch = RESULT
- if ch = "RESULT" then ch = " "
- end
- 'SetMark'
- 'CopyBlock 1'
- 'DeleteBlock'
- 'Goto' a.x a.y /* and insert Loc(MSG_?) instead */
- 'GetString (Locale structure name:) (MSG_)'
- if RC ~= 0 then name = "MSG_?"
- else name = RESULT
- 'WriteText "Loc('||name||')"'
-
- call gettextfile /* open the file containing locals */
- if ret ~= 0 then return
-
- if (OldID ~= 0) then
- do
- 'LockWindow' OldID /* leave the cursor untouched, if the */
- 'Goto' a.x a.y /* locals are in a different file */
- 'LockWindow' ID
- end
-
- 'Goto 1 1' /* get maximum catalog message number */
- 'SetVar (_FindString) (#define LOCALE_INDEX)'
- 'Find @CURSOR @CURSOR @EOT @EOT {@SILENT}'
- if RC ~= 0 then
- do
- 'Goto 1 1'
- 'WriteText (#define LOCALE_INDEX 1)'
- 'Return'
- idx = 0
- end
- else do
- 'GetVar (_CurrentLine)' /* increment and replace msg num */
- line = RESULT
- idx = word(line,3)
- 'DeleteLine'
- 'WriteText "#define LOCALE_INDEX' idx+1 || '"'
- 'Return'
- end
- 'Goto 1 1' /* add msg to end of locale block */
- 'SetVar (_FindString) (#define LOCALE_END)'
- 'Find @CURSOR @CURSOR @EOT @EOT {@SILENT}'
- 'Goto @SOL @SOL'
- 'Return'
- 'CursorUp 0'
- 'WriteText "struct LocText' name||'={'||idx||',"'
- 'InsertBlock 1'
- 'Goto @EOL @EOL'
- 'WriteText "};"'
- 'Return'
- 'WriteText "/*"'
- 'Return'
- 'WriteText "*/"'
- 'Return'
- return
-
- gettextfile: procedure EXPOSE ID OldID ret
-
- 'Goto 1 1' /* find locale tag */
- 'SetVar (_FindString) ($Localefile)'
- 'Find @CURSOR @CURSOR @EOT @EOT {@SILENT}'
- if RC = 0 then
- do
- 'GetVar (_CurrentLine)' /* parse it */
- line = RESULT
- line = substr(line,pos("$Localefile",line))
- line = delword(line,1,1)
- do i=1 until cnt = 0
- ch = substr(line,i,1)
- if ((ch = ':') | (ch = '=') | (ch = ' ')) then cnt = 1
- else cnt = 0
- end
- line = substr(line,i)
- line = word(line,1)
-
- sfil = reverse(line) /* isolate filename */
- i = pos('/',sfil) /* to compare with _File */
- if i ~= 0 then sfil = left(sfil,i-1) /* variable */
- i = pos(':',sfil)
- if i ~= 0 then sfil = left(sfil,i-1)
- sfil = reverse(sfil)
-
- 'GetVar (_CurrentID)' /* store ID */
- OldID = RESULT
-
- LockWindow 0 /* we must of course unlock */
- 'NextED 0' /* file already loaded ? */
- file = ""
- do while ((RC = 0) & (file ~= sfil))
- 'GetVar (_CurrentID)'
- ID = RESULT
- 'GetVar (_File)'
- file = RESULT
- NextED ID
- end
-
- if file = sfil then
- do
- LockWindow ID /* lock and activate window */
- 'Goto 1 1'
- 'GetVar (_WinMode)'
- if ((RESULT = 1) | (RESULT = 2)) then 'Window 0 0 0 0'
- end
- else do
- LockWindow OldID /* if not: get path and open */
- 'GetVar (_FILEPATH)'
- path = RESULT
- if right(path,1,':') ~= ':' then path = path || '/'
-
- if ~exists(line) then /* check file existance */
- do
- if ~exists(path||line) then
- do
- 'MessageOK "Could not open localefile:' line '"'
- ret = 10
- return
- end
- line = path||line
- end
-
- 'NewEd ""' /* open the tagged file */
- ID = RESULT
- 'LockWindow' ID
- 'Open "'||line||'"'
- 'Window 300 14 0 300'
- end
- end
- else OldID = 0 /* code: SAME_FILE */
-
- ret = 0
- return
-
- fixstring: procedure EXPOSE ret
-
- do until ret ~= 0
- call catstring int
- end
- 'Goto 1 _YPos'
- return
-
- breakstring: procedure
-
- 'GetVar (_XPos)' /* home position */
- max = RESULT
- 'GetVar (_YPos)'
- y.o = RESULT
- y.c = y.o
-
- do forever
- 'SetRexxClip (WRITELineClip) 0 _YPos' /* seek spaces and '\n' in line to break there */
- line = getclip('WRITELineClip')
- p = pos("\n",line)
- if ((p ~= 0) & (p ~> max)) then w.o=p+2 /* break behind '\n' */
- else if (length(line) > max) then
- do
- w.c = 0
- w.o = 0
- do i=2 while w.c ~> max /* find words fitting in line */
- w.o = w.c
- w.c = wordindex(line,i)
- end
- if (w.o = 0) then w.o = w.c /* overfull hbox penalty ... */
- end
- else w.o=0
-
- if (w.o = 0) then /* somethings wiered */
- do
- 'Goto' max y.o
- return 0
- end
-
- line = strip(right(line,w.o)) /* does the string continue ? */
- if substr(line,1,1) ~= '"' then
- do
- 'Goto' w.o y.c /* cut it and recurse next line */
- call cutstring
- y.c = y.c + 1
- end
- else do
- 'Goto' max y.o /* DONE */
- return 0
- end
- end
- return 0
-
- cutstring: procedure
-
- 'GETVAR (_XPos)' /* get home position */
- a.x = RESULT
- 'GETVAR (_YPos)'
- a.y = RESULT
-
- 'GetVar (_CurrentLine)' /* seek '"' for autoindent position */
- line = RESULT
- b.x = pos('"',line)
- if b.x = 0 then /* cannot format def'd strings !*/
- do
- 'MessageOK "Sorry, but this line does not hold any string!"'
- return 0
- end
-
- line = substr(line,a.x) /* get leading spaces, because */
- spc = wordindex(line,1)-1 /* "RETURN" will kill them ! */
-
- 'WriteText ("\)' /* cut string */
- 'Return'
- 'GETVAR (_XPOS)'
- b.c = RESULT
- b.d = b.x - b.c
- if b.d > 0 then /* autoindent */
- do
- line = copies(' ',b.d)
- 'WriteText "'||line||'"'
- end
- 'WriteChar 34' /* begin next '"' */
- if spc > 0 then
- do
- line = copies(' ',spc) /* restore killed spaces */
- 'WriteText "'||line||'"'
- end
-
- return 0
-
- catstring: procedure EXPOSE ret
-
- arg mode
-
- 'SetRexxClip (WRITELineClip) 0 _YPos'
- line = getclip('WRITELineClip')
- line = strip(line,'T')
- if right(line,1) = "\" then 'Goto' length(line) '_YPos'
- else
- do
- p = pos("\ ",line)
- if p = 0 then
- do
- if mode = "EXT" then 'MessageOK "Sorry, but this line does not hold a broken string!"'
- ret = 5
- return
- end
- 'Goto' p '_YPos'
- end
-
- 'DELETE' /* kill cat operator */
- ch = ' '
- do while ch = ' ' /* find last real symbol */
- 'CursorLeft 0'
- if RC ~= 0 then
- do
- 'MessageOK "Sorry, but this line does not hold a broken string!\nNote that the line has been changed."'
- ret = RC
- return
- end
- 'GETVAR (_CurrentChar)'
- ch = RESULT
- if ch = "RESULT" then ch = " "
- end
- if ch ~= '"' then 'CursorRight 0'
- 'DeleteToEOL' /* kill overhead */
- do until ch ~= ' ' /* and autoindent spaces */
- 'Delete'
- 'GetVar (_CurrentChar)'
- ch = RESULT
- end
- if ch = '"' then 'DELETE' /* and of course the '"' */
-
- ret = 0
- return
-
-